var pageSize = 10;
var pageNum = 1;
var page;
var laypage;



$(function () {
    $("#menuId").val(sessionStorage.menuId);
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate','element','laypage','layer'], function(){
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入

        var start = {
            // min: laydate.now(),
            max: '2099-06-16 23:59:59'
            ,istoday: true
            ,choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            // min: laydate.now(),
            max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };

        document.getElementById('LAY_demorange_s').onclick = function(){
            start.elem = this;
            laydate(start);
        }
        document.getElementById('LAY_demorange_e').onclick = function(){
            end.elem = this
            laydate(end);
        }
    });
    setTimeout(function () {
        findAgentIncomeList();
    },200);

});



//查询代理商收益列表
function findAgentIncomeList(){
    var menuId = $("#menuId").val();
    var startTime = $("#LAY_demorange_s").val();
    var endTime = $("#LAY_demorange_e").val();
    var phone = $("#phone").val();

    var params = {
        "type":"query",
        "menuId":menuId,
        "pageSize":pageSize,
        "pageNum":pageNum,
        "startTime":startTime,
        "endTime":endTime,
        "phone":phone
    };
    $.ajax({
        type:"post",
        url: "/agent/findAgentIncomeList",
        data:params,
        dataType: "json",
        success : function(data){
            if(data.code = 1 && data.code != undefined){
                var list = data.agentIncomeDetailNews;
                page = data.page;
                var html = "";
                for(var b in list){

                    html += '<tr><td>'+list[b].agentName+'</td>' +
                        '<td>'+list[b].agentLevelStr+'</td>' +
                        '<td>'+list[b].businessAdvertIncome+'</td>' +
                        '<td>'+list[b].areaAdvertIncome+'</td>' +
                        '<td>'+list[b].recommendedIncome+'</td>' +
                        '<td>'+list[b].consumerIncome+'</td>' +
                        '<td>'+list[b].sumIncome +'</td>' +
                        '<td><button onclick="findIncomeDetail('+list[b].incomeUserId+')" class="layui-btn layui-btn-normal">查看明细</button></td></tr>'
                }
                $("#bus").html(html);
                $("#total").html("共有数据："+page.total+" 条");
                laypage({
                    cont: 'page'
                    ,pages: page.pages
                    ,first: 1
                    ,curr:pageNum
                    ,last: page.pages
                    ,prev: '<em><</em>'
                    ,next: '<em>></em>'
                    ,jump: function (obj,first) {
                        pageNum = obj.curr;
                        if(!first){
                            findAgentIncomeList();
                        }
                    }
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }else if(data.code = 2 && data.code != undefined){
                $("#bus").html("");
            }
        }

    });
}

//查询按钮
function search() {
    pageNum = 1;
    findAgentIncomeList();
}
//查看明细
function findIncomeDetail(userId) {
    sessionStorage.incomeUserId = userId;
    layer.open({
        title: ['收益明细','color: #2299ee;border-bottom: 1px solid;']
        ,type: 2
        ,content: 'agentIncomeDetail.html'
        ,area: ['100%', '100%']
        ,maxmin:true
    });
}
